package monitor

import (
	"html/template"
	"net/http"

	//	"strconv"
	//	"strings"
	"time"
)

import (
	//	"buyChance"
	"r_log"
	//	"util"
)

/*
处理指令页面的

*/
type Command struct {
	ExceTime string
	//	State    [4]bool
	PassWord string
}

func (c *Command) init() {
	//	c.State = util.BuychanceSwitch
}

func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	t := template.Must(template.ParseFiles(monitorunit.MonitorConf.CommandAddr))
	c.ExceTime = time.Now().String()
	// 解析表单
	err := r.ParseForm()
	if err != nil {
		r_log.Logger.Warn("getCommandReload parseForm is faild: %v", err)
	}
	// 密码验证
	if pwd, ok := r.Form["password"]; ok && pwd[0] == c.PassWord {
		/*if _, onReload := r.Form["Reload"]; onReload {
			bcb := &buyChance.BCBase
			for p := util.PERIOD_DAY; p <= util.PERIOD_HHOUR; p++ {
				bcb.BcLock[p].Lock()
				bcb.BuyChances[p].Date = 0
				bcb.BuyChances[p].Time = 0
				//bcb.BuyChances[p].Valid = false
				bcb.BuyChances[p].BcMap = make(map[util.StkID]*buyChance.BuyChance)
				bcb.BuyChances[p].BcSlice = make([]*buyChance.BuyChance, 0)
				bcb.BcLock[p].Unlock()
			}
			bcdate := uint32(util.Today())
			bctime := uint32(999999)
			bcb.DdsSocket.InitBc(bcdate, bctime)
			var strainerdate, strainertime uint32
			bcb.DdsSocket.InitStrainer(strainerdate, strainertime)
			c.ExceTime = time.Now().String() + "  Reload Complete !!!"
		}

			if enabler, okEnable := r.Form["enable"]; okEnable {
				periodString := strings.Split(enabler[0], "_")[1]
				period, err := strconv.Atoi(periodString)
				if err != nil {
					r_log.Logger.Error("strconv atoi is failed! %v", err)
				}
				util.EnableBC(util.Period(period))
				c.ExceTime = time.Now().String() + "  Enable" + periodString + " Period Complete !!!"
			}

			if disabler, okDisable := r.Form["disable"]; okDisable {
				periodString := strings.Split(disabler[0], "_")[1]
				period, err := strconv.Atoi(periodString)
				if err != nil {
					r_log.Logger.Error("strconv atoi is failed! %v", err)
				}
				util.DisableBC(util.Period(period))
				c.ExceTime = time.Now().String() + "  Disable" + periodString + " Period Complete !!!"
			}*/
	} else {
		c.ExceTime = time.Now().String() + "  PassWord is err !!!"
	}
	c.init()
	err = t.Execute(w, c)
	if err != nil {
		r_log.Logger.Warn("StateInfo template execute is faild: %v", err)
	}
}
